using System;
using System.Xml;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework.Configuration;
using Microsoft.Practices.RecipeFramework.Library;

namespace VSSDKAssist.Recipes.Common.Coordinators
{
	/// <summary>
	/// Conditional Action coordinator that executes an Action based on Recipe Boolean Argument
	/// </summary>
	public class ConditionalExecutionProgressCoordinator : ExecutionProgressCoordinator
	{
		#region Constants
		private const string ExecuteConditionAttribute = "ExecuteCondition";
		#endregion

		/// <summary>
		/// Executes the action.
		/// </summary>
		/// <param name="action">The action.</param>
		protected override void ExecuteAction(Microsoft.Practices.RecipeFramework.Configuration.Action action)
		{
			XmlAttribute executeCondition;

			if(FindAttribute(action.AnyAttr, ExecuteConditionAttribute, out executeCondition))
			{
				bool execute = false;

				//TODO: Add support for ActionOutputs
				string argumentValue = new ExpressionEvaluationService().Evaluate(executeCondition.Value, new ServiceAdapterDictionary(DictionaryService)).ToString();

				Boolean.TryParse(argumentValue, out execute);

				if(execute)
				{
					base.ExecuteAction(action);
				}
			}
			else
			{
				base.ExecuteAction(action);
			}
		}
	}
}